<?php
namespace common\models\order;

use common\models\member\Address;
use common\models\member\MemberInfo;
use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use common\enums\StatusEnum;
use common\helpers\RegularHelper;

/**
 * This is the model class for table "{{%order}}".
 *
 */
class Order extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%order}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['order_sn','store_id','buyer_id','payment_code'], 'required'],
            [['order_sn','pay_sn','store_id','buyer_id','created_at','payment_time','finnshed_time','evaluation_state','order_state','refund_state','lock_state','delay_time','order_from'], 'integer'],
            [['goods_amount', 'order_amount','pd_amount','shipping_fee','refund_amount'], 'number'],
            [['store_name','buyer_name','shipping_code'], 'string', 'max' => 50],
            [['payment_code'],'string','max'=>10],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'order_id' => 'ID',
            'order_sn'=>'订单编号',
            'pay_sn' => '支付单号',
            'store_id' => '卖家店铺id',
            'store_name' => '卖家店铺名称',
            'buyer_id' => '买家id',
            'buyer_name' => '买家姓名',
            'created_at' => '订单生成时间',
            'payment_code' => '支付方式名称代码',
            'payment_time' => '支付(付款)时间',
            'finnshed_time' => '订单完成时间',
            'goods_amount'=>'商品总价格',
            'order_amount'=>'订单总价格',
            'pd_amount' => '预存款支付金额',
            'shipping_fee'=>'运费',
            'evaluation_state'=>'评论状态',// 0未评论 1已评论
            'order_state'=>'订单状态',//订单状态：0(已取消)10(默认):未付款;20:已付款;30:已发货;40:已收货;
            'refund_state'=>'退款状态',//退款状态:0是无退款,1是部分退款,2是全部退款
            'lock_state'=>'锁定状态',//锁定状态:0是正常,大于0是锁定,默认是0
            'refund_amount'=>'退款金额',
            'delay_time'=>'延迟时间,默认为0',
            'order_from'=>'订单端口',//1WEB2mobile
            'shipping_code'=>'物流单号',

        ];
    }

    /**
     * 场景
     *
     * @return array
     */
    public function scenarios()
    {
        return [
//            'backendCreate' => ['goods_name', 'goods_code','goods_bar','goods_unit','gc_id','member_id','store_id'],
            'default' => array_keys($this->attributeLabels()),
        ];
    }


    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAddress(){
        return $this->hasOne(Address::className(),['id'=>'address_id'])
            ->select(['id','address_name','address_details','realname','mobile']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getBuyer(){
        return $this->hasOne(MemberInfo::className(),['id'=>'buyer_id'])
            ->select(['id','nickname','username']);
    }

    /**
     * 获取订单商品
     * @return \yii\db\ActiveQuery
     */
    public function getGoods(){
        return $this->hasMany(OrderGoods::className(),['order_id'=>'order_id'])
            ->select(['order_id','goods_id','goods_name','goods_price','goods_num','goods_image']);
    }

    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ];
    }
}
